/**
 * ===========================================================================
 * 版权所有 湖南免了么网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.mianle.me
 * ===========================================================================
 * 11:11 2018/7/9
 */
package com.mianle.userapi.service.delayed.demo1;

import java.util.Date;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @Author: yiyuan
 * @Description:
 * @Date: Created in 11:11 2018/7/9
 *
 */
public class Event2 implements Delayed {


    private Date startDate;

    public Event2(Date startDate) {
        this.startDate = startDate;
    }

    @Override
    public int compareTo(Delayed o) {
        // TODO Auto-generated method stub
        long result = this.getDelay(TimeUnit.NANOSECONDS)
                - o.getDelay(TimeUnit.NANOSECONDS);
        if (result < 0)
            return -1;
        else if (result > 0)
            return 1;
        return 0;
    }

    /**
     * 返回到激活日期的剩余时间，单位由参数指定
     */
    @Override
    public long getDelay(TimeUnit unit) {
        // TODO Auto-generated method stub
        Date nowDate = new Date();
        long diff = startDate.getTime() - nowDate.getTime();
        return unit.convert(diff, TimeUnit.MILLISECONDS);

    }

}
